<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
window.jsTestIsAsync = true;
description("Tests that properties of various events do not leak between isolated worlds (bug 85158).");

function addListener(eventType, prop) {
    document.addEventListener(eventType, function(event) {
        documentObject = event[prop];

        // Object passed into isolated world should be undefined.
        shouldBeNull("documentObject");

        // The property defined in the isolated world should be undefined.
        shouldBeUndefined("document.pageDefinedVar");

        window.postMessage("done", "*");
    });
}

function sendDocumentObject(eventType, prop) {
    var newEvent = eval("new " + eventType + "('" + eventType + "', { " + prop + ": document })");
    document.dispatchEvent(newEvent);
}

function runScript(eventType, prop) {
    // Final string should have the form:
    //     document.pageDefinedVar = 1; (function sendDocumentObject(eventType, prop) {...})(...);
    // When evaluated in the isolated world, should initiate the event with the
    // document object as the specificed property value.
    var script = "document.pageDefinedVar = 1; " +
                 "(" + sendDocumentObject.toString() + ")('" + eventType + "', '" + prop + "');";
    addListener(eventType, prop);
    testRunner.evaluateScriptInIsolatedWorld(1, script);
}

// Run the tests whenever a notification arrives, which indicates that the
// previous test has finished.
window.addEventListener("message", function(message) {
    runNextTest();
}, false);

// The events that we want to test, with the properties that each one uses.
var events = [
    { eventType: "CustomEvent", prop: "detail" },
    { eventType: "MessageEvent", prop: "data" },
    { eventType: "PopStateEvent", prop: "state" }
];

function runNextTest () {
    var evt = events.pop();
    if (!evt) {
        finishJSTest();
        return;
    }

    runScript(evt.eventType, evt.prop);
};

// This test is meaningless without testRunner.
if (window.testRunner) {
    runNextTest();
}
</script>
</body>
</html>
